package panics

import (
	"github.com/618lf/swakx-go/pkg/logger"
	perrors "github.com/pkg/errors"
	"runtime/debug"
)

var log = logger.GetLogger()

// CatchPanicWithHandleError 恢复错误处理
func CatchPanicWithHandleError(f func(error)) {
	if err := recover(); err != nil {
		err := perrors.Errorf("recover panic. error:%v, stack: %s", err, debug.Stack())
		if log != nil {
			log.Error(err)
		}
		if f != nil {
			f(err)
		}
	}
}

// CatchPanic 恢复错误处理
func CatchPanic() {
	if err := recover(); err != nil {
		if log != nil {
			log.Errorf("recover panic. error:%v, stack: %s", err, debug.Stack())
		}
	}
}
